package com.terabits.smartriver.service.impl;

import com.terabits.smartriver.mapper.RegionMapper;
import com.terabits.smartriver.meta.vo.RegionVO;
import com.terabits.smartriver.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Yin on 2019/3/7
 */
@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<RegionVO> getRegionCascadedAll() {
        return regionMapper.selectRegionCascadedAll();
    }

    //-1:全部 0或者不传:有值的省市区 1:企业 2:项目 3:设备 4:所有设备名称 5：所有设备类型 6：盒子 7:街道 此时isSimple为区的id
    @Override
    public List getRegionCascaded(Integer type, Integer isSimple, String roleProjectNum) {
        String sql = "";

        if (isSimple != 0) {
            switch (type) {
                case 1:
                    if ("null".equals(roleProjectNum)) {
                        sql = "SELECT id,enterprise_num enterpriseNum,name FROM enterprise ORDER BY CONVERT(name USING GBK) ASC";
                    } else {
                        sql = "SELECT enterprise.id,enterprise.enterprise_num enterpriseNum,enterprise.name FROM enterprise LEFT JOIN project ON project.enterprise_id=enterprise.id where project.project_num=" + "'" + roleProjectNum + "'" + " ORDER BY CONVERT(enterprise.name USING GBK) ASC";
                    }
                    break;
                case 2:
                    sql = "SELECT id,project_num,name FROM project";
                    break;
                case 3:
                    sql = "SELECT id,device_num,device_name FROM device";
                    break;
                case 4:
                    sql = "SELECT device_name FROM device GROUP BY device_name";
                    break;
                case 5:
                    sql = "SELECT type FROM device GROUP BY type";
                    break;
                case 6:
                    sql = "SELECT id,box_num name FROM box";
                    break;
                case 7:
                    sql = "SELECT id value,name label,0 disabled FROM sys_region WHERE pid='" + isSimple + "'";
                    break;
                default:
                    return null;
            }
            return regionMapper.selectListSQL(sql);
        }

        //非简单的
        switch (type) {
            case 0:
                if ("null".equals(roleProjectNum)) {
                    sql = "SELECT province_id id,getRegion(province_id) value,city_id id2,getRegion(city_id) value2,district_id id3,getRegion(district_id) value3,street_id id4,getRegion(street_id) value4,id id5 ,name value5 FROM enterprise";
                } else {
                    sql = "SELECT enterprise.province_id id,getRegion(enterprise.province_id) value,enterprise.city_id id2,getRegion(enterprise.city_id) value2,enterprise.district_id id3,getRegion(enterprise.district_id) value3,enterprise.street_id id4,getRegion(enterprise.street_id) value4,enterprise.id id5,enterprise.town value5 FROM enterprise LEFT JOIN project ON project.enterprise_id=enterprise.id where project.project_num='" + roleProjectNum + "'";
                }
                break;
            case 1:
                if ("null".equals(roleProjectNum)) {
                    sql = "SELECT province_id id,getRegion(province_id) value,city_id id2,getRegion(city_id) value2,district_id id3,getRegion(district_id) value3,street_id id4,getRegion(street_id) value4,id id5 ,name value5 FROM enterprise";
                } else {
                    sql = "SELECT enterprise.province_id id,getRegion(enterprise.province_id) value,enterprise.city_id id2,getRegion(enterprise.city_id) value2,enterprise.district_id id3,getRegion(enterprise.district_id) value3,enterprise.street_id id4,getRegion(enterprise.street_id) value4,enterprise.id id5,enterprise.town value5 FROM enterprise LEFT JOIN project ON project.enterprise_id=enterprise.id where project.project_num='" + roleProjectNum + "'";
                }
                break;
            case 2:
                if ("null".equals(roleProjectNum)) {
                    sql = "SELECT enterprise_id id, (SELECT name FROM enterprise WHERE id = enterprise_id) value, id id2, name value2 FROM project";
                } else {
                    sql = "SELECT enterprise_id id, (SELECT name FROM enterprise WHERE id = enterprise_id) value, id id2, name value2 FROM project where project.project_num='" + roleProjectNum + "'";
                }
                break;
            case 3:
                sql = "SELECT enterprise.id id,enterprise.NAME value,project_id id2,project.NAME value2,device.id id3,device_num value3 FROM device LEFT JOIN project ON project.id=project_id LEFT JOIN enterprise ON enterprise.id=enterprise_id;";
                break;
            default:
                return null;
        }
        return regionMapper.selectCascaded(sql);

    }

}
